package com.ecode.annotation.builder.mysql;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ecode.annotation.EcodeDo;
import com.ecode.annotation.ErrorCode;
import com.ecode.annotation.core.CodeDataPullBuilder;

import java.sql.*;

/**
 * @ClassName MysqlCodeDataPullBuilder
 * @Author mjlft
 * @Date 2021/10/4 11:55
 * @Version 1.0
 * @Description mysql数据拉取构建器
 */
public class MysqlCodeDataPullBuilder implements CodeDataPullBuilder {

    @Override
    public String dataPull(EcodeDo ecodeDo) {
        final String DB_URL = ecodeDo.getMysqlUrl();
        final String USER = ecodeDo.getMysqlUserName();
        final String PASS = ecodeDo.getMysqlUserPass();
        final String tableName = ecodeDo.getMysqlTableName();
        if (DB_URL == null || "".equals(DB_URL.trim()) ||
                USER == null || "".equals(USER.trim()) ||
                PASS == null || "".equals(PASS.trim()) ||
                tableName == null || "".equals(tableName.trim())) {
            throw new RuntimeException("数据库配置确实");
        }
        String QUERY = "SELECT sys, model, version, code, msg FROM " + ecodeDo.getMysqlTableName() + " WHERE 1 = 1";
        int index = 0;
        if (ecodeDo.getSys() != null && !"".equals(ecodeDo.getSys().trim())) {
            QUERY += " AND sys = ?";
            index++; //1
            if (ecodeDo.getModel() != null && !"".equals(ecodeDo.getModel().trim())) {
                QUERY += " AND model = ?";
                index++; //2
                if (ecodeDo.getVersion() != null && !"".equals(ecodeDo.getVersion().trim())) {
                    QUERY += " AND version = ?";
                    index++; //3
                }
            }
        }
        // Open a connection
        JSONArray jsonArray = new JSONArray();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.prepareStatement(QUERY);
            if (index > 0) {
                stmt.setString(1, ecodeDo.getSys());
            }
            if (index > 1) {
                stmt.setString(2, ecodeDo.getModel());
            }
            if (index > 2) {
                stmt.setString(3, ecodeDo.getVersion());
            }
            rs = stmt.executeQuery();
            // Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("sys", rs.getString("sys"));
                jsonObject.put("model", rs.getString("model"));
                jsonObject.put("version", rs.getString("version"));
                jsonObject.put("code", rs.getString("code"));
                jsonObject.put("msg", rs.getString("msg"));
                jsonArray.add(jsonObject);
            }
        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("通过数据库获取错误码配置失败");
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return jsonArray.toJSONString();
    }
}
